/**
 * 初始化页面
 */
$(function () {
    var id = sessionStorage.getItem('id');
    // console.log(id);
    //发异步，从Java获取数据
    $.ajax({
        type: 'get',
        url: requestURL+prefix+userService+'/role/findById/'+id,
        headers: {"Authorization":authToken},
        success: function (data) {
            if (data.status == 200 ) {
                //赋值
                $("#L_id").val(data.object.id);
                $("#L_name").val(data.object.name);
                $("#description").val(data.object.description);
                $("#L_sort").val(data.object.sort);
                return false;
            }
            // console.log(data);
            layer.msg('信息获取失败，请稍后重试！',{icon:0,time:1200});
            return false;
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            var state = XMLHttpRequest.status;
            if (state == 403) {
                layui.layer.msg('权限不足，请联系超级管理员！', {icon: 0, time: 1200});
                return false;
            }
            layui.layer.msg('请求接口异常！', {icon: 0, time: 1200});
            return false;
        }

    });
});

layui.use(['form', 'layer', 'laydate', 'jquery'], function () {
    $ = layui.jquery;
    var form = layui.form,
        laydate = layui.laydate;
    layer = layui.layer;

    //自定义验证规则
    form.verify({
        name: function (value) {
            if (value.length < 2) {
                return '角色名称至少得2个字符';
            }
        },
    });

    //监听提交
    form.on('submit(add)', function (data) {
        // console.log(data.field);
        //发异步，把数据提交给Java
        $.ajax({
            type: 'post',
            url: requestURL+prefix+userService+'/role/updateMessage',
            headers: {"Authorization":authToken},
            data: JSON.stringify(data.field),
            contentType: 'application/json;charset=utf-8',
            dataType: 'json',
            success: function (res) {
                if (res.status == 200 ) {
                    $("#sumbit").attr("disabled",true); //禁止点击按钮
                    layer.msg('修改成功！', {icon: 1, time: 1100,end:function () {
                            // 可以对父窗口进行刷新
                            xadmin.father_reload();
                        }});
                    return false;
                }
                layer.msg('修改失败！', {icon: 2, time: 1200});
                return false;
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                var state = XMLHttpRequest.status;
                if (state == 403) {
                    layui.layer.msg('权限不足，请联系超级管理员！', {icon: 0, time: 1200});
                    return false;
                }
                layui.layer.msg('请求接口异常！', {icon: 0, time: 1200});
                return false;
            }

        });
        return false;
    });

});

/**
 * 重置会员编辑表单信息为原始信息
 */
function resetAdminEdit() {
    var id = sessionStorage.getItem('id');
    // console.log(id);
    //发异步，从Java获取数据
    $.ajax({
        type: 'get',
        url: requestURL+prefix+userService+'/role/findById/'+id,
        headers: {"Authorization":authToken},
        success: function (data) {
            if (data.status == 200 ) {
                //赋值
                $("#L_id").val(data.object.id);
                $("#L_name").val(data.object.name);
                $("#description").val(data.object.description);
                $("#L_sort").val(data.object.sort);
                return false;
            }
            if (data.status == 403) {
                layui.layer.msg('权限不足，请联系超级管理员！', {icon: 0, time: 1200});
                return false;
            }
            // console.log(data);
            layer.msg('信息获取失败，请稍后重试！',{icon:0,time:1200});
            return false;
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            var state = XMLHttpRequest.status;
            if (state == 403) {
                layui.layer.msg('权限不足，请联系超级管理员！', {icon: 0, time: 1200});
                return false;
            }
            layui.layer.msg('请求接口异常！', {icon: 0, time: 1200});
            return false;
        }
    });
}